var color=[["#fe8e08","#ffc501","#1da6da","#5dc3c7"],["#5dc3c7","#61d2fe","#37a4dd","#b39ddb","#fec400","#fe8e08"]];

$(function(){
	$(".selectLine a").click(function(){
		resultAjax(getJsPath()+"../comm/taskSegment.json","lineId="+$(this).attr("id"),setTaskSegment,"JSON");

		getSendBall($(this).attr("id"));
	});


	$(".exportExlBtn").click(function(){
		$('.zy-i-input').css({border:"none"});
		$('.zy-i-input').css({height:"35px"});
		var lineName=window.line.val('text');
		var taskName=window.task.val('text');
		var name = lineName+taskName+$(this).attr("exportName");


		resultAjax(path+"/comm/checkOutTime.json","",function(data,name){
			var exportFile = $(this).EXPT({
				name:name ,
				yesBtnAct: function(callback){
					exportPDF(exportFile.name);
					callback();
				}
			});

		},"json",name);

	});




	$(".find").click(function(){
		window.isDownPDF=true;
		searchData();
	});
	tabHandler();
	
	$(".defect-down img").each(function(){
		if($(this).attr("style") != undefined)
		{
			$(this).parent().attr("imgsty",$(this).attr("style"))
		}
		$(this).parent().css("text-align","center");
		$(this).parent().attr("img",path+"/static/img/chart_no_data.png");
		$(this).attr("src",path+"/static/img/chart_no_data.png")
	});
	
	
	$(".right-report.clearfix div[id]").each(function(){
		if($(this).parent().hasClass("w100"))
		{
			$(this).css("width",$(".right-report.clearfix").width()-30);
			$(this).css("height","174px");
		}else{

			$(this).css("width",($(".right-report.clearfix").width()-50)/2);
		}
	});
	bindBall();
});

exportPDF=function(fileName){
	if(window.isDownPDF)
	{
		var args=[];
		$(".defect-down").each(function(){
			args.push(this);
		});
		var lineName=window.line.val('text');
		var taskName=window.task.val('text');

		var obj = [window.line,window.task,window.begin,window.end];

		if(checkForm(obj)&&checkFormVerify($("#JQ_form")))
		{
			downPDF(args, lineName + taskName + "金属损失统计图", resetData,fileName);
		}
	}
}



function searchData()
{


	var lineIdVal=window.line.val();
	var taskIdVal=window.task.val();
	var beginMileageVal=window.begin.val('mile');
	var endMileageVal=window.end.val('mile');


	var obj = [window.line,window.task,window.begin,window.end];

	if(checkForm(obj)&&checkFormVerify($("#JQ_form")))
	{
		var data="lineId="+lineIdVal+"&taskId="+taskIdVal;
		data+="&beginMileage="+beginMileageVal+"&endMileage="+endMileageVal;
		resultAjax(getJsPath()+"../insp/statistics/metallossStat/search.json",data,findResult,"JSON");
	}


	return true;
}


/**
 * 处理tab 点击事件
 */
function tabHandler(){
	$(".left-table table tr td a").attr("href","javascript:;");
	$(".left-table table tr td a").live("click",function(){
		

		var lineId=window.line.val();
		var taskId=window.task.val();
		var beginMileage=window.begin.val('mile');
		var endMileage=window.end.val('mile');

		if($(this).attr("to") != undefined && typeof(lineId) != "undefined" &&
		   typeof(taskId) != "undefined" && lineId != ""
		   && taskId != ""
		){
			var url=$(this).attr("to");//+"&lineId="+lineId+"&taskId="+taskId+"&beginMileage="+beginMileage+"&endMileage="+endMileage;
			var param=[];
			param.push({name:"lineId",value:lineId});
			param.push({name:"taskId",value:taskId});
			param.push({name:"beginMileage",value:beginMileage});
			param.push({name:"endMileage",value:endMileage});
			param.push({name:"beginMileageName",value:window.begin.val("text")});
			param.push({name:"endMileageName",value:window.end.val("text")});
			if($(this).text() != "0"){
				//alert("ssss");
				//window.location.href=url;
				postGo(url,param)
			}
			
		}
		
		
	});
}

function parseData(val)
{

	return  val == null?"0":val;
}
function resetData()
{
	findResult(window.prevData);
}

/**
 * 查询数据并返回
 * @param data
 */
function findResult(data)
{
	window.prevData=data;
	if(data.result != undefined)
	{
		data=data.result;
	}

	var pie={};
	pie.tab1={obj:"ball"};
	console.log(data);
	var totle=0;
	$("#allmt tr").each(function(index){
		if(index <=5 && index >0)
		{
			var num = parseInt(parseData(data["homeList"][index-1]["NUM"]));
			if(index <5)
			{
				totle+=num;
			}
			
			
			$(this).find("td:eq(1) a").html(num);
			if(pie.tab1.name == undefined )
			{
				pie.tab1.name=[];
				pie.tab1.data=[];
			}
			if(index <=4 && index >0 ){
				pie.tab1.data.push({name:$(this).find("td:eq(0)").text().substring(0,6),value:num,color:color[0][index-1]});
				pie.tab1.name.push($(this).find("td:eq(0)").text().substring(0,6));
			}
			
		}else{
			if(totle >0)
			{
				$(this).find("td:eq(1) a").html(totle==0?0:1);
			}
		}
		
	});
	$("#allmt tr:eq(0) td:eq(1) a").html(totle);
	
	totle=0;
	pie.tab2={obj:"ba"};
	$("#MTNS tr").each(function(index){
		if(index < 8 && index >0)
		{
			
			//console.log(data["tab1List"].length);
			console.log(data["tab1List"][index]);
			var num = parseInt(parseData(data["tab1List"][index-1]["NUM"]));
			if(index <7)
			{
				totle+=num;
			}
			$(this).find("td:eq(1) a").html(num);
			
			if(pie.tab2.name == undefined )
			{
				pie.tab2.name=[];
				pie.tab2.data=[];
			}
			if(index <=6 && index >0 ){
				pie.tab2.data.push({name:$(this).find("td:eq(0)").text(),value:num,color:color[1][index-1]});
				pie.tab2.name.push($(this).find("td:eq(0)").text());
			}
		}else{
			if(totle >0)
			{
				$(this).find("td:eq(1) a").html(totle==0?0:1);
			}
		}
		
	});
	$("#MTNS tr:eq(0) td:eq(1) a").html(totle);
	totle=0;
	pie.tab3={obj:"bb"};
	$("#MTWS tr").each(function(index){
		if(index <8 && index >0)
		{
			var num = parseInt(parseData(data["tab2List"][index-1]["NUM"]));
			if(index <7)
			{
				totle+=num;
			}
			$(this).find("td:eq(1) a").html(num);
			
			if(pie.tab3.name == undefined )
			{
				pie.tab3.name=[];
				pie.tab3.data=[];
			}
			if(index <=6 && index >0 ){
				pie.tab3.data.push({name:$(this).find("td:eq(0)").text(),value:num,color:color[1][index-1]});
				pie.tab3.name.push($(this).find("td:eq(0)").text());
			}
		}else{
			if(totle >0)
			{
				$(this).find("td:eq(1) a").html(totle==0?0:1);
			}
		}	
	});
	$("#MTWS tr:eq(0) td:eq(1) a").html(totle);
	totle=0;
	pie.tab4={obj:"bc"};
	$("#mtnx tr").each(function(index){
		if(index <8 && index >0)
		{
			var num = parseInt(parseData(data["tab3List"][index-1]["NUM"]));
			if(index <7)
			{
				totle+=num;
			}
			$(this).find("td:eq(1) a").html(num);
			if(pie.tab4.name == undefined )
			{
				pie.tab4.name=[];
				pie.tab4.data=[];
			}
			if(index <=6 && index >0 ){
				pie.tab4.data.push({name:$(this).find("td:eq(0)").text(),value:num,color:color[1][index-1]});
				pie.tab4.name.push($(this).find("td:eq(0)").text());
			}
		}else{
			if(totle >0)
			{
				$(this).find("td:eq(1) a").html(totle==0?0:1);
			}
		}
		
	});
	$("#mtnx tr:eq(0) td:eq(1) a").html(totle);
	totle=0;
	pie.tab5={obj:"bd"};
	$("#MTWX tr").each(function(index){
		if(index <8 && index >0)
		{
			var num = parseInt(parseData(data["tab4List"][index-1]["NUM"]));
			if(index <7)
			{
				totle+=num;
			}
			$(this).find("td:eq(1) a").html(num);
			if(pie.tab5.name == undefined )
			{
				pie.tab5.name=[];
				pie.tab5.data=[];
			}
			if(index <=6 && index >0 ){
				pie.tab5.data.push({name:$(this).find("td:eq(0)").text(),value:num,color:color[1][index-1]});
				pie.tab5.name.push($(this).find("td:eq(0)").text());
			}
		}else{
			if(totle >0)
			{
				$(this).find("td:eq(1) a").html(totle==0?0:1);
			}
		}
		
	});
	$("#MTWX tr:eq(0) td:eq(1) a").html(totle);
	var picECharts = new PicEChart();
	for( var item in pie)
	{
		var num=0;
		for(var n in pie[item].data)
		{
			num+=pie[item].data[n].value;
			pie[item].data[n].name = pie[item].data[n].name.replace("数量","");
		}

		if(num >0)
		{
			picECharts.addHollowPieByML({legends:pie[item].name,legendX:"47%",legendY:"15%",data:pie[item].data,divObject:pie[item].obj});
			$("#"+pie[item].obj).parent().find("i").remove();
		}else{
			$("#"+pie[item].obj).html("<img src='"+$("#"+pie[item].obj).attr("img")+"' />");
		}
		
	}

	drawTimeCharts(data.timeList,"#szs",parseTimeData(data.timeList),"",function(d){});
	drawTimeCharts(data.tab1TimeList,"#sza",parseTimeData(data.tab1TimeList),"",function(d){});
	drawTimeCharts(data.tab2TimeList,"#szb",parseTimeData(data.tab2TimeList),"",function(d){});
	drawTimeCharts(data.tab3TimeList,"#szc",parseTimeData(data.tab3TimeList),"",function(d){});
	drawTimeCharts(data.tab4TimeList,"#szd",parseTimeData(data.tab4TimeList),"",function(d){});
	
	drawBar(data.lineList,"tzt");
	drawBar(data.tab1LineList,"tza");
	drawBar(data.tab2LineList,"tzb");
	drawBar(data.tab3LineList,"tzc");
	drawBar(data.tab4LineList,"tzd");
	
	
}

/**
 * 绘制柱状图
 * @param data
 * @param box
 */
function drawBar(data,box)
{
	//$("#"+box).attr("img",path+"/static/chart_no_data.png")

	$('#'+box).css("height",$('#'+box).parent().height()-70);
	$('#'+box).parent().find(".dw").remove();
	var html_='<em class="dw">(m)</em>';
	var totale=0;
	var Axisdata=[];
	var showdata=[];
	for(var i=0;i<data.length;i++){
		Axisdata.push(parseFloat(data[i].NAME).toFixed(0));
		var num =data[i].NUM;
		num=num==null?"0":num;
		num=parseInt(num);
		showdata.push(num);
		totale+=num;
	}
	if(totale >0 )
	{
		var picECharts = new PicEChart();
		//picECharts.addbar({name1:'降水量',Axisdata:Axisdata,data:showdata,divObject:box});
		$("#"+box).parent().find("i").remove();
		$("#"+box).css({position:"relative"});
		window.setTimeout(function(){$('#'+box).append(html_)},500);

		drawClickBar(Axisdata,showdata,box);

	}else{
		$("#"+box).html("<img src='"+$("#"+box).attr("img")+"'  />");
		//$("#"+box).parent().append("<i></i>");
	}
	
}

/**
 * 解析时钟方位数据
 * @param data
 * @returns {*}
 */
function parseTimeData(data)
{
	for(var item in data)
	{
		data[item].t=parseInt(data[item].NAME)+1;
		data[item].val=data[item].NUM==null?0:data[item].NUM;
		data[item].color=parseInt(data[item].NAME);
	}
	return data;
}

/**
 * 绘制时钟方位图形
 * @param data
 * @param id
 */
function drawTimeCharts(data,id)
{
	var count = 0;
	var clickJson=[];

	var h12=0;
	for(var i=0;i<data.length;i++){
		var t=data[i].NAME;
		var num =data[i].NUM;
		num= (num=="-"|| num==null )?"0":num;
		if(t != "12")
		{
			clickJson.push({"t":(parseInt(t)+1),"val":num,"color":parseInt(t)});
		}else{
			h12=num;
		}
		count += num;
	}

	for(var i=0;i<clickJson.length;i++)
	{
		if(clickJson[i].t==1)
		{
			clickJson[i].val=parseInt(clickJson[i].val)+parseInt(h12);
		}
	}

	if(count == 0)
	{
		$(id).html("<img src='"+$(id).attr("img")+"'  />");
	}else{
		//$(id).css({width:})
		new createTimeMap(id,clickJson,"",function(d){});

	}
}



drawClickBar=function(mileageList,data,id)
{
	mileageList = handlerMileage(mileageList);
	//$("#"+id).css("width",$("#"+id).parent().width()-20);
	var picECharts = new PicEChart();
	picECharts.addMorebar({name1:'对比统计图',num:1,Axisdata:mileageList,data:data,divObject:id,legend:mileageList,onClickCall:function() {

	}});
};


window.isbh = "_";//修改了echarts 源代码


function handlerMileage(data1)
{
	var mileageList = [];
	var t = null;
	var ts = null;
	for(var i=0,e=data1.length;i<e;i++)
	{
		var v = Math.round(parseFloat(data1[i]))+"";
		if( i+1 <e )
		{
			if(parseFloat(data1[i+1])  - parseFloat(data1[i])  <10)
			{

				// v=v.substring(0, v.length) ;
			}else if(parseFloat(data1[i+1])  - parseFloat(data1[i])  <100)
			{

				v=v.substring(0, v.length -1) +"0";
			}else if(parseFloat(data1[i+1])  - parseFloat(data1[i])  >=100)
			{
				v=v.substring(0, v.length -2) +"00";
			}
			t = data1[i];
		}else{
			if(parseFloat(data1[i])  - parseFloat(t)  <10)
			{

				// v=v.substring(0, v.length) ;
			}else if(parseFloat(data1[i])  - parseFloat(t)  <100)
			{
				v=v.substring(0, v.length -1) +"0";

			}else if(parseFloat(data1[i])  - parseFloat(t)  >=100)
			{
				v=v.substring(0, v.length -2) +"00";

			}
		}

		if(ts != v)
		{
			t = v;
			mileageList.push(parseFloat(v));
		}else{
			mileageList.push("");
		}
		ts =v;
	}

	return mileageList;
}
